/**
 * 
 */
package de.idgard.android.addon.backend.impl;

import android.content.Context;

import com.google.web.bindery.event.shared.SimpleEventBus;
import com.google.web.bindery.requestfactory.vm.RequestFactorySource;

import de.idgard.android.addon.backend.IDGBackendService;
import de.idgard.android.addon.backend.IDGServiceRegistry;
import de.idgard.android.addon.services.transport.LoginRequestTransport;
import de.idgard.webapp.user.client.service.LoginServiceRF;
import de.idgard.webapp.user.client.service.LoginServiceRF.LoginService;

/**
 * 
 * @author <a href="mailto:vesko.georgiev@uniscon.com">Vesko Georgiev</a>
 */
public final class BackendServiceRegistry extends IDGServiceRegistry {

	private static BackendServiceRegistry instance;

	private BackendServiceRegistry() {

	}

	public void resetContext(Context ctx) {
		// empty..
	}

	public static BackendServiceRegistry instance() {
		if (instance == null) {
			instance = new BackendServiceRegistry();
		}
		return instance;
	}

	private IDGBackendService backendService;
	private Context context;
	private boolean init = false;

	public void init(Context ctx) {
		if (init) {
			return;
		}
		this.context = ctx;

		setContext(backendService = new IDGBackendServiceImpl(), ctx);

		init = true;
	}

	private LoginServiceRF getRequestFactory() {
		LoginServiceRF requestFactory = RequestFactorySource
				.create(LoginServiceRF.class);
		requestFactory.initialize(new SimpleEventBus(),
				new LoginRequestTransport(context));
		return requestFactory;
	}

	public IDGBackendService getBackendService() {
		return backendService;
	}

	public LoginService getLoginService() {
		return getRequestFactory().loginService();
	}

}
